package com.dingtalk.dingbotmgr.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.dingtalk.dingbotmgr.model.Attachment;
import com.dingtalk.dingbotmgr.model.Attachment.Type;
import com.dingtalk.dingbotmgr.model.Project;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Created by wyx on 2017/2/28.
 */
@Component("attachmentAction")
@Scope("prototype")
public class AttachmentAction extends BaseAction {

    private static final String UPLOAD_PATH = "/upload";

    private Attachment attachment;

    private List<Attachment> attachmentList;

    private Project project;

    private File file;

    private String fileFileName;

    private String fileContentType;

    private String getUploadRoot() {
        return ServletActionContext.getServletContext().getRealPath(UPLOAD_PATH);
    }

    public String listAttachment() {
        project = projectService.getEntityById(project.getId());
        attachmentList = attachmentService.list();
        return SUCCESS;
    }

    public InputStream getDownloadFile() {
        return ServletActionContext.getServletContext().getResourceAsStream(UPLOAD_PATH + "/" + attachment.getId());
    }

    public String downloadAttachment() {
        attachment = attachmentService.getEntityById(attachment.getId());
        if (attachment.getType() != Type.FILE) {
            return ERROR;
        }
        fileFileName = attachment.getFileName();
        return SUCCESS;
    }

    public String saveAttachment() throws IOException {
        if (attachment.getType() == Type.FILE && file != null) {
            if (StringUtils.isEmpty(attachment.getName())) {
                attachment.setName(fileFileName);
            }
            attachment.setFileName(fileFileName);
        }
        attachmentService.saveOrUpdate(attachment);
        if (attachment.getType() == Type.FILE && file != null) {
            String name = String.valueOf(attachment.getId());
            FileUtils.copyFile(file, new File(getUploadRoot(), name));
        }
        project = attachment.getProject();
        return SUCCESS;
    }

    public String deleteAttachment() {
        attachment = attachmentService.getEntityById(attachment.getId());
        project = attachment.getProject();
        attachmentService.delete(attachment);
        return SUCCESS;
    }

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }

    public List<Attachment> getAttachmentList() {
        return attachmentList;
    }

    public void setAttachmentList(List<Attachment> attachmentList) {
        this.attachmentList = attachmentList;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
}
